﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="Order.cs" company="Open Trader">
//   Copyright (c) David Denis (david.denis@systemathics.com)
// </copyright>
// <summary>
//   |  Open Trader - The Open Source Systematic Trading Platform
//   |
//   |  This program is free software: you can redistribute it and/or modify
//   |  it under the terms of the GNU General Public License as published by
//   |  the Free Software Foundation, either version 2 of the License, or
//   |  (at your option) any later version.
//   |
//   |  This program is distributed in the hope that it will be useful,
//   |  but WITHOUT ANY WARRANTY; without even the implied warranty of
//   |  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//   |  GNU General Public License for more details.
//   |
//   |  You should have received a copy of the GNU General Public License
//   |  along with this program.  If not, see http://www.gnu.org/licenses
//   |
//   |  Up to date informations about Open Trader can be found at :
//   |    http://opentrader.org
//   |    http://opentrader.codeplex.com
//   |
//   |  For professional services, please visit us at :
//   |    http://www.systemathics.com
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Org.OpenTrader.Extensions.ReportBuilder
{
    #region Using Directives

    using System;
    using System.Collections.Generic;

    using Org.OpenTrader.Framework.Entities;

    #endregion

    /// <summary>
    /// The order.
    /// </summary>
    public class Order
    {
        #region Constants and Fields

        /// <summary>
        /// The contents.
        /// </summary>
        private readonly List<OrderContent> contents = new List<OrderContent>();

        /// <summary>
        /// The db instrument.
        /// </summary>
        private readonly DBInstrumentStaticData dbInstrument;

        /// <summary>
        /// The db order.
        /// </summary>
        private readonly DBOrder dbOrder;

        #endregion

        #region Constructors and Destructors

        /// <summary>
        /// Initializes a new instance of the <see cref="Order"/> class.
        /// </summary>
        /// <param name="dbOrder">
        /// The db order.
        /// </param>
        /// <param name="dbInstrument">
        /// The db instrument.
        /// </param>
        /// <param name="id">
        /// The id.
        /// </param>
        public Order(DBOrder dbOrder, DBInstrumentStaticData dbInstrument, int id)
        {
            this.dbOrder = dbOrder;
            this.dbInstrument = dbInstrument;
            this.ID = id;

            this.dbOrder.DBOrderStatus.Load();
            foreach (var db_status in this.dbOrder.DBOrderStatus)
            {
                if (db_status.Status != "Execution")
                {
                    var content = new OrderContent(db_status);
                    this.contents.Add(content);
                }
            }

            this.dbOrder.DBOrderExecution.Load();
            foreach (var db_exec in this.dbOrder.DBOrderExecution)
            {
                var content = new OrderContent(db_exec);
                this.contents.Add(content);
            }

            this.contents.Sort(new OrderContent.Comparer());
        }

        #endregion

        #region Properties

        /// <summary>
        /// Gets BookingPath.
        /// </summary>
        public string BookingPath
        {
            get
            {
                return this.dbOrder.Path;
            }
        }

        /// <summary>
        /// Gets ID.
        /// </summary>
        public int ID { get; private set; }

        /// <summary>
        /// Gets InstrumentCode.
        /// </summary>
        public string InstrumentCode
        {
            get
            {
                return this.dbInstrument.Code;
            }
        }

        /// <summary>
        /// Gets InstrumentScope.
        /// </summary>
        public string InstrumentScope
        {
            get
            {
                return this.dbInstrument.TradingScope;
            }
        }

        /// <summary>
        /// Gets OrderContents.
        /// </summary>
        public OrderContent[] OrderContents
        {
            get
            {
                return this.contents.ToArray();
            }
        }

        /// <summary>
        /// Gets Price.
        /// </summary>
        public double Price
        {
            get
            {
                return this.dbOrder.Price;
            }
        }

        /// <summary>
        /// Gets Quantity.
        /// </summary>
        public int Quantity
        {
            get
            {
                return this.dbOrder.Quantity;
            }
        }

        /// <summary>
        /// Gets TimeStamp.
        /// </summary>
        public DateTime TimeStamp
        {
            get
            {
                return this.dbOrder.TimeStamp;
            }
        }

        /// <summary>
        /// Gets Type.
        /// </summary>
        public string Type
        {
            get
            {
                return this.dbOrder.Type;
            }
        }

        /// <summary>
        /// Gets Way.
        /// </summary>
        public string Way
        {
            get
            {
                return this.dbOrder.Way;
            }
        }

        #endregion
    }
}